package edu.mtu.citizenscience.cs.sdk;

import edu.mtu.citizenscience.cs.sdk.json.Response;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * A base for the ResponeHandler to a request to the server
 * extended in a class that needs to make a request to the server.
 * 
 * When extended it should be parameterized by the expected
 * payload type.
 */
public abstract class ResponseHandler<E> {
	// the type of the expected payload
	private Class<?> playloadType;
	
	/**
	 * Create the Response Handler
	 * 
	 * @param   playloadType - Class<?> - The class type of the expected payload
	 * @return	void
	 */
	public ResponseHandler(Class<?> playloadType)
	{
		// save the payload type
		this.playloadType = playloadType;
	}
	
	/**
	 * Called on a valid response from the server
	 * 
	 * @param   response - Response<E> - The response from the server
	 * @return	void
	 */
	public abstract void response(Response<E> response);
	
	/**
	 * Called on a error from the server
	 * 
	 * @param   response - Response<E> - The response from the server
	 * @return	void
	 */
	public abstract void error(Response<E> response);
	
	/**
	 * Called on no response from the server
	 * 
	 * @param   void
	 * @return	void
	 */
	public abstract void noResponse();
	
	/**
	 * Called on an exception in parsing the response from the server into JSON
	 * 
	 * @param   void
	 * @return	void
	 */
	public void exception(Exception exception)
	{
		try
		{
			// throw the passed exception
			throw exception;
		}
		catch(Exception e)
		{
			// display the exception to the log
			e.printStackTrace();
		}
	}
	
	/**
	 * Get the expected payload type for this response
	 * 
	 * @param   void
	 * @return	void
	 */
	public Class<?> getPlayloadType() 
	{
		// return the payload type
		return playloadType;
	}

}
